package com.wwj.tem.config;
import cn.dev33.satoken.stp.StpInterface;
import com.wwj.tem.mapper.SysRoleMapper;
import com.wwj.tem.mapper.SysUserMapper;
import com.wwj.tem.service.SysRolePermissionService;
import com.wwj.tem.service.SysUserRoleService;
import com.wwj.tem.service.impl.SysPermissionServiceImpl;
import com.wwj.tem.service.impl.SysRoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: wwj
 * @create: 2024/7/24 - 21:43
 * Description: 自定义权限加载接口实现类
 */
@Component
public class StpInterfaceImpl implements StpInterface {
	@Resource
	private SysUserRoleService sysUserRoleService;
	
	@Resource
	private SysRolePermissionService sysRolePermissionService;
	
	/**
	 * 获取用户权限列表
	 */
	@Override
	public List<String> getPermissionList(Object loginId, String loginType) {
		// 1. 根据用户ID获取角色ID列表
		Long userId = Long.valueOf(loginId.toString());
		List<Long> roleIds = sysUserRoleService.getRoleIdsByUserId(userId);
		
		// 2. 根据角色ID列表获取权限编码列表
		return sysRolePermissionService.getPermissionCodesByRoleIds(roleIds);
	}
	
	/**
	 * 获取用户角色列表（如果需要@SaCheckRole注解可实现此方法）
	 */
	@Override
	public List<String> getRoleList(Object loginId, String loginType) {
		Long userId = Long.valueOf(loginId.toString());
		return sysUserRoleService.getRoleCodesByUserId(userId);
	}
}

